package cx.zan.web.servlet;

import cx.zan.domain.PageBean;
import cx.zan.domain.Product;
import cx.zan.service.ProductService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/4/1 9:44
 */
@WebServlet("/product")
public class ProductServlet extends BaseServlet {
    private ProductService productService = new ProductService();

    /**
     * 查询热门商品
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     */
    public void findhot(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        List<Product> hotlist = productService.findhot();
        //ResultVo resultVo = new ResultVo(ResultVo.CODE_SUCCESS,hotlist,"查询成功！");
        //
        //writeJson(resultVo);
        writeSuccess(hotlist);
    }

    /**
     * 根据分类cid进行商品查询
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     */
    public void findProductByCid(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        String cid = request.getParameter("cid");
        //查询那一页 ，页码
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //一页 多少条数据
        int pageSize = 12;

        //直接传pageBean对象
        PageBean<Product> pageBean = new PageBean();
        pageBean.setPageNumber(pageNumber);
        pageBean.setPageSize(pageSize);
        PageBean<Product> pagelist = productService.findProductByCidPageBean(cid, pageBean);

        //List<Product> list = productService.findProductByCid(cid);
        //ResultVo resultVo = new ResultVo(ResultVo.CODE_SUCCESS,hotlist,"查询成功！");
        //
        //writeJson(resultVo);
        writeSuccess(pagelist);
    }

    /**
     * 根据商品pid查询
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     */
    public void findById(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        String pid = request.getParameter("pid");
        Product product = productService.findById(pid);

        writeSuccess(product);
    }

    /**
     * 查询所有
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     */
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
       List<Product> list = productService.findAll();
       writeSuccess(list);
    }

}
